<template>
  <!-- 加工单计划设备明细表单弹窗 -->
  <div>
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      :title="props.title"
      width="900px"
      draggable
      @close="closeDialog"
      destroy-on-close
    >
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="100">
        <el-row>
          <el-col :span="12">
            <el-form-item label="加工单号" prop="workOrderCode">
              <el-input
                v-if="!props.isReadonly && !formData.id"
                v-model="formData.workOrderCode"
                placeholder="请输入加工单号"
                @click="showWorkOrderSelect"
              />
              <!-- <el-select
                v-model="formData.workOrderId"
                v-if="!props.isReadonly && !formData.id"
                :disabled="!formData.workCenterId"
                placeholder="请选择加工单号"
                clearable
                filterable
                style="width: 100%"
                @change="changeWorkOrder"
              >
                <el-option
                  v-for="item in workOrderList"
                  :key="item.id"
                  :label="`${item.workOrderCode}`"
                  :value="item.id"
                />
              </el-select> -->
              <span v-else>{{ formData.workOrderCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工厂" prop="plantCode">
              <el-input
                v-if="!props.isReadonly && !formData.id"
                v-model="formData.plantCode"
                placeholder="请输入工厂"
                disabled
                clearable
              />
              <!-- <el-select
                @keyup.enter="formData"
                v-model="formData.plantId"
                placeholder="请选择工厂"
                v-if="!props.isReadonly && !formData.id"
                clearable
                style="width: 100%"
                @change="selectPlant"
              >
                <el-option
                  v-for="item in plantList"
                  :key="item.id"
                  :label="item.organizationCode"
                  :value="item.id"
                />
              </el-select> -->
              <span v-else>{{ formData.plantCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作中心" prop="workCenterId">
              <el-input
                v-if="!props.isReadonly && !formData.id"
                v-model="formData.workCenters"
                placeholder="请输入工厂"
                disabled
                clearable
              />
              <!-- <el-select
                v-model="formData.workCenterId"
                placeholder="请选择工作中心"
                v-if="!props.isReadonly && !formData.id"
                clearable
                :disabled="!formData.plantId"
                @change="changeWorkCenter"
                filterable
                style="width: 100%"
              >
                <el-option
                  v-for="item in workShopList"
                  :key="item.id"
                  :label="`${item.workshopCode}-${item.workshopName}`"
                  :value="item.id"
                />
              </el-select> -->
              <span v-else
                >{{ formData.workCenterCode }}-{{
                  formData.workCenterName
                }}</span
              >
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作年" prop="planYear">
              <el-input
                v-if="!props.isReadonly && !formData.id"
                v-model="formData.planYear"
                placeholder="请输入工作年"
                disabled
                clearable
              />
              <!-- <el-select
                v-model="formData.planYear"
                v-if="!props.isReadonly && !formData.id"
                :disabled="!formData.workCenterId"
                placeholder="请选择工作年份"
                clearable
                @change="selectYear"
                style="width: 100%"
              >
                <el-option
                  v-for="item in yearOptions"
                  :key="item"
                  :value="Number(item)"
                  :label="item"
                />
              </el-select> -->
              <span v-else>{{ formData.planYear }}年</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作周" prop="planWeek">
              <el-input
                v-if="!props.isReadonly && !formData.id"
                v-model="formData.planWeek"
                placeholder="请输入工作周"
                disabled
                clearable
              />
              <!-- <el-select
                v-if="!props.isReadonly && !formData.id"
                v-model="formData.planWeek"
                :disabled="!formData.workCenterId"
                placeholder="请选择工作周"
                clearable
                @change="selectWeek"
                style="width: 100%"
              >
                <el-option
                  v-for="item in monthOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select> -->
              <span v-else>{{ formData.planWeek }}周</span>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="生产批次" prop="planBatch">
              <el-input
                v-if="!props.isReadonly && !formData.id"
                v-model="formData.planBatch"
                disabled
                placeholder="请输入生产批次"
                clearable
              />
              <span v-else>{{ formData.planBatch }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="日期" prop="planDate">
              <el-input
                v-if="!props.isReadonly && !formData.id"
                v-model="formData.planDates"
                placeholder="请输入工作周"
                disabled
                clearable
              />
              <!-- <el-select
                v-model="formData.planDate"
                v-if="!props.isReadonly && !formData.id"
                :disabled="!formData.workOrderCode"
                placeholder="请选择日期"
                clearable
                @change="selectDay"
                style="width: 100%"
              >
                <el-option
                  v-for="item in dayList"
                  :key="item"
                  :value="item.planDate"
                  :label="dayjs(item.planDate).format('YYYY-MM-DD')"
                />
              </el-select> -->

              <span v-else>{{
                dayjs(formData.planDate).format("YYYY-MM-DD")
              }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班次顺序" prop="workSequence">
              <el-input
                v-if="!props.isReadonly && !formData.id"
                v-model="formData.workSequences"
                placeholder="请输入班次顺序"
                disabled
                clearable
              />
              <!-- <el-select
                v-model="formData.workSequence"
                v-if="!props.isReadonly && !formData.id"
                :disabled="!formData.planDate && !formData.workOrderCode"
                placeholder="请选择班次顺序"
                clearable
                style="width: 100%"
                @change="selectShift"
              >
                <el-option
                  v-for="item in shiftList"
                  :key="item"
                  :value="item.shiftCode"
                  :label="item.shiftName"
                />
              </el-select> -->
              <span v-else>{{ formData.workSequence }}班</span>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="计划数量" prop="planQuantity">
              <el-input
                v-if="!props.isReadonly && !formData.id"
                v-model="formData.planQuantity"
                placeholder="请输入计划数量"
                disabled
                clearable
              />

              <span v-else>{{ formData.planQuantity }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="总完成数量" prop="planAllQuantity">
              <el-input
                v-if="!props.isReadonly && !formData.id"
                v-model="formData.planAllQuantity"
                placeholder="请输入总完成数量"
                clearable
                disabled
              />

              <span v-else>{{ formData.planAllQuantity }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工序" prop="processId">
              <el-select
                v-model="formData.processId"
                v-if="!props.isReadonly && !formData.id"
                :disabled="!formData.workOrderCode"
                placeholder="请选择工序"
                clearable
                filterable
                style="width: 100%"
                @change="changeProcess"
              >
                <el-option
                  v-for="item in processList"
                  :key="item.id"
                  :label="`${item.processCode}-${item.processName}`"
                  :value="item.processId"
                />
              </el-select>
              <span v-else
                >{{ formData.processCode }}-{{ formData.processName }}</span
              >
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备" prop="equipmentId">
              <el-select
                v-model="formData.equipmentId"
                v-if="!props.isReadonly && !formData.id"
                :disabled="!formData.processId"
                placeholder="请选择设备"
                clearable
                filterable
                style="width: 100%"
                @change="changeEquipment"
              >
                <el-option
                  v-for="item in equipmentList"
                  :key="item.id"
                  :label="`${item.equipmentCode}`"
                  :value="item.equipmentId"
                />
              </el-select>
              <span v-else>{{ formData.equipmentCode }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="完成数量" prop="planFinishQuantity">
              <el-input
                v-if="!props.isReadonly"
                v-model="formData.planFinishQuantity"
                placeholder="请输入完成数量"
                clearable
              />
              <span v-else>{{ formData.planFinishQuantity }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button
            v-if="!props.isReadonly"
            type="primary"
            :loading="loading"
            @click="handleSubmit"
            >确 定</el-button
          >
          <el-button
            v-if="!formData.id"
            type="primary"
            :loading="loading"
            @click="continueSubmitting"
            >继续添加</el-button
          >
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <mesworkorderequipplan-select
      :visible="selectDialogSELECT.visible"
      @change-visible="changeVisibleSELECT"
      :brekList="brekList"
    />
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();
import {
  getPlantList,
  getFirstWorkShopList,
  getWorkShopList,
  getMesRouteGroupList,
  getWorkCenterDetail,
  getCurrentYield,
  getFreeDetails,
  getMonthList,
  getYearList,
  getMaterialList,
  getDateshiftCount,
} from "@/utils/commonData";
import {
  getWeeksInYear,
  getWeekNumber,
  week_date,
  getDaysOfCurrentWeek,
} from "@/utils/dateUtils";
import type { TableColumnCtx } from "element-plus";
import { convertToChineseNumber, validateNumber } from "@/utils/commonHooks";
import {
  getOneLineDate,
  WeekDetailType,
  SpanMethodProps,
  SearchType,
  resolveSearchData,
  resolveSaveData,
  ToChineseDate,
  getCurrentWeekDetail,
  objectSpanMethod,
  calculateLoadRate,
  getSummaries,
  mockData,
  getNameInfo,
  deepCopy,
} from "@/views/mes/weeklyProducePlan/index";
import dayjs from "dayjs";
defineOptions({
  name: "MESMESWORKORDEREQUIPPLANADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);
// 小数校验
function validator1(rule, value, callback: (a?: any) => {}) {
  if (!value) {
    //先判断空可以过
    callback(new Error("请输入完成数量"));
  } else {
    if (/^\d{0,8}$/.test(value)) {
      //验证正整数和0的正则
      return true;
    } else {
      callback(new Error("请输入大于0的整数"));
    }
  }
}
const rules = reactive({
  plantId: [{ required: true, message: "请选择工厂", trigger: "change" }],
  workCenterId: [
    { required: true, message: "请选择工作中心", trigger: "change" },
  ],
  planYear: [{ required: true, message: "请选择工作年份", trigger: "change" }],
  planWeek: [{ required: true, message: "请选择工作周", trigger: "change" }],
  workOrderCode: [
    { required: true, message: "请选择加工单号", trigger: "change" },
  ],
  processId: [{ required: true, message: "请选择工序", trigger: "change" }],
  equipmentId: [{ required: true, message: "请选择设备", trigger: "change" }],
  planDate: [{ required: true, message: "请选择日期", trigger: "change" }],
  workSequence: [
    { required: true, message: "请选择班次顺序", trigger: "change" },
  ],
  planQuantity: [],
  planAllQuantity: [],
  planFinishQuantity: [
    // {
    //   validator: validator1,
    //   trigger: "blur",
    // },
    {
      message: "请输入大于0的整数",
      trigger: "blur",
      pattern: /^[1-9]\d*$/,
    },
    { required: true, message: "请输入完成数量", trigger: "blur" },
  ],
  quantityUnit: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);
const currentYear = new Date().getFullYear(); // 当前年份
const currentWeek = getWeekNumber(new Date()); // 当前第几周
const classCount = 3; // 固定三个班次
let formData: any = reactive({
  id: "",
  plantId: "",
  plantName: "",
  plantCode: "",
  workCenterId: "",
  workCenterName: "",
  workCenterCode: "",
  planYear: currentYear,
  planWeek: currentWeek,
  workOrderId: "",
  workOrderName: "",
  workOrderCode: "",
  planBatch: "",
  planDate: "",
  workSequence: "",
  planQuantity: 0,
  planAllQuantity: 0,
  processId: "",
  processCode: "",
  processName: "",
  equipmentId: "",
  equipmentCode: "",
  equipmentName: "",
  planFinishQuantity: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
  mesWorkOrderEquipId: "",
  mesWorkOrderPlanId: "",
  quantityUnit: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        plantId,
        plantName,
        plantCode,
        workCenterId,
        workCenterName,
        workCenterCode,
        planYear,
        planWeek,
        workOrderId,
        workOrderName,
        workOrderCode,
        planBatch,
        planDate,
        workSequence,
        planQuantity,
        planAllQuantity,
        processId,
        processCode,
        processName,
        equipmentId,
        equipmentCode,
        equipmentName,
        planFinishQuantity,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        mesWorkOrderEquipId,
        mesWorkOrderPlanId,
        quantityUnit,
      } = newVal;
      Object.assign(formData, {
        id,
        plantId,
        plantName,
        plantCode,
        workCenterId,
        workCenterName,
        workCenterCode,
        planYear,
        planWeek,
        workOrderId,
        workOrderName,
        workOrderCode,
        planBatch,
        planDate,
        workSequence,
        planQuantity,
        planAllQuantity,
        processId,
        processCode,
        processName,
        equipmentId,
        equipmentCode,
        equipmentName,
        planFinishQuantity,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        mesWorkOrderEquipId,
        mesWorkOrderPlanId,
        quantityUnit,
      });

      // getTable();
      // getDataDay();
      // workShopList1();
      // monthOptions.value = getMonthList(formData.planYear);
    }
  }
);
// 编辑时需要获取的表格数据
// const selectDay1 = (e) => {
//   dayList.value.forEach((item) => {
//     if (item.planDate == e) {
//       shiftCount.value = item.shiftCount;
//     }
//   });

//   for (let i = 0; i < shiftCount.value; i++) {
//     shiftList.value.push({ shiftName: i + 1 + "班", shiftCode: i + 1 });
//   }
//   getFinishQuantity();
// };

// 获取编辑完成数量
// const getFinishQuantity = async () => {
//   await request({
//     url: "/mes/mesworkorderplan/getList",
//     method: "post",
//     data: {
//       planWeek: formData.planWeek,
//       planYear: formData.planYear,
//       workOrderId: formData.workOrderId,
//       plantId: formData.plantId,
//       planBatch: formData.planBatch,
//       planDate: formData.planDate,
//       workSequence: formData.workSequence,
//     },
//   }).then((res) => {
//     if (res.code == 200) {
//       formData.planQuantity = res.data[0].planQuantity;
//       formData.planAllQuantity = res.data[0].planFinishQuantity;
//     }
//   });
// };

// 编辑时需要获取的表格数据
// const workShopList1 = async () => {
//   workShopList.value = (await getWorkShopList(formData.plantId)) ?? [];
// };
// 编辑时需要获取的表格数据
// const selectProcess1 = (e) => {
//   tableData.value.forEach((item) => {
//     if (item.id == e) {
//       oneTableList.value.push(item);
//       getProcessList(item);
//       equList(formData.processId);
//     }
//   });
// };

// const equList = (e) => {
//   oneTableList.value.forEach((item) => {
//     item.workOrderEquipList.forEach((item1) => {
//       if (item1.processId == e) {
//         equipmentList.value = [];
//         equipmentList.value.push(item1);
//       }
//     });
//   });
// };
/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.mesWorkOrderEquipId = "";
  formData.plantId = "";
  formData.plantName = "";
  formData.plantCode = "";
  formData.workCenterId = "";
  formData.workCenterName = "";
  formData.workCenterCode = "";
  formData.planYear = currentYear;
  formData.planWeek = currentWeek;
  formData.workOrderId = "";
  formData.workOrderName = "";
  formData.workOrderCode = "";
  formData.planBatch = "";
  formData.planDate = "";
  formData.workSequence = "";
  formData.planQuantity = 0;
  formData.planAllQuantity = 0;
  formData.processId = "";
  formData.processCode = "";
  formData.processName = "";
  formData.equipmentId = "";
  formData.equipmentCode = "";
  formData.equipmentName = "";
  formData.planFinishQuantity = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.mesWorkOrderEquipId = "";
  formData.mesWorkOrderPlanId = "";
  formData.quantityUnit = "";
  formData.workCenters = "";
  formData.workSequences = "";
  formData.planDates = "";
  hh.value = true;
}

const loading = ref(false);

/** 加工单计划设备明细保存提交 */
function handleSubmit() {
  console.log(formData);

  formRef.value.validate(async (valid: any) => {
    if (valid) {
      if (
        formData.planFinishQuantity * 1 > formData.planAllQuantity * 1 ||
        formData.planFinishQuantity * 1 == 0
      ) {
        ElMessage.error("完成数量不能超过总数量");
        return;
      }
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      console.log("一开始", formData);
      await request({
        url: "/mes/mesworkorderequipplan/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

function continueSubmitting() {
  console.log("继续", formData);

  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesworkorderequipplan/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      formData.planFinishQuantity = "";
      formData.equipmentId = "";
      formData.equipmentCode = "";
      formData.equipmentName = "";
    }
  });
}

// 工厂编号列表
const plantList: any = ref([]);

// 工作中心列表
const workShopList: any = ref([]);
// 当前选中的工厂
// async function selectPlant(e: string) {
//   workShopList.value = (await getWorkShopList(e)) ?? [];
//   if (e) {
//     clearPlantList();
//     plantList.value.forEach((item) => {
//       if (item.id == e) {
//         formData.plantName = item.organizationName;
//         formData.plantCode = item.organizationCode;
//       }
//     });
//   } else {
//     clearPlantList();
//     formData.plantName = "";
//     formData.plantCode = "";
//   }
// }

// clearPlantList
const clearPlantList = () => {
  formData.plantName = "";
  formData.plantCode = "";
  formData.mesWorkOrderEquipId = "";
  formData.workCenterId = "";
  formData.workCenterName = "";
  formData.workCenterCode = "";
  formData.planYear = currentYear;
  formData.planWeek = currentWeek;
  formData.workOrderId = "";
  formData.workOrderName = "";
  formData.workOrderCode = "";
  formData.planBatch = "";
  formData.planDate = "";
  formData.workSequence = "";
  formData.planQuantity = 0;
  formData.planAllQuantity = 0;
  formData.processId = "";
  formData.processCode = "";
  formData.processName = "";
  formData.equipmentId = "";
  formData.equipmentCode = "";
  formData.equipmentName = "";
  formData.planFinishQuantity = "";
  formData.mesWorkOrderPlanId = "";
  formData.quantityUnit = "";
};
// 获取周列表
const monthOptions = ref<OptionType[]>();

// 获取年度列表
const yearOptions = ref();

// 年度变更重新获取月份
// function selectYear(e: number) {
//   clearYearList();
//   if (e) {
//     monthOptions.value = getMonthList(e);
//   } else {
//     monthOptions.value = [];
//   }
// }

// 改变年之后清空
// function clearYearList() {
//   formData.planWeek = "";
//   formData.planDate = "";
//   formData.workOrderId = "";
//   formData.workOrderName = "";
//   formData.workOrderCode = "";
//   formData.workSequence = "";
//   formData.planBatch = "";
//   formData.planQuantity = 0;
//   formData.planAllQuantity = 0;
//   formData.processId = "";
//   formData.processCode = "";
//   formData.processName = "";
//   formData.equipmentId = "";
//   formData.equipmentCode = "";
//   formData.equipmentName = "";
//   formData.planFinishQuantity = "";
//   formData.mesWorkOrderEquipId = "";
//   formData.mesWorkOrderPlanId = "";
//   formData.quantityUnit = "";
// }

// 获取订单编号数据
const tableData = ref([]);
// 对订单数据进行处理
const workOrderList = ref([]);
// 周数变更重新获取月份
// function selectWeek(e: number) {
//   clearWeekList();
//   if (e) {
//     getTable();
//   } else {
//     tableData.value = [];
//     workOrderList.value = [];
//   }
// }
// 改变年之后清空
// function clearWeekList() {
//   formData.planDate = "";
//   formData.workOrderId = "";
//   formData.workOrderName = "";
//   formData.workOrderCode = "";
//   formData.planBatch = "";
//   formData.planQuantity = 0;
//   formData.planAllQuantity = 0;
//   formData.workSequence = "";
//   formData.processId = "";
//   formData.processCode = "";
//   formData.processName = "";
//   formData.equipmentId = "";
//   formData.equipmentCode = "";
//   formData.equipmentName = "";
//   formData.planFinishQuantity = "";
//   formData.mesWorkOrderEquipId = "";
//   formData.mesWorkOrderPlanId = "";
//   formData.quantityUnit = "";
// }
// 选完周之后发送请求
let hh = ref(true);
// const getTable = async () => {
//   tableData.value = [];
//   await request({
//     url: "/mes/mesworkorder/queryByWeek",
//     method: "post",
//     data: {
//       planWeek: formData.planWeek,
//       planYear: formData.planYear,
//       workCenterId: formData.workCenterId,
//       plantId: formData.plantId,
//       planBatch: formData.planBatch,
//     },
//   })
//     .then((res) => {
//       tableData.value = res.data;
//       workOrderList.value = res.data;
//       // 编辑时触发一次
//       if (formData.id) {
//         if (hh.value) {
//           selectProcess1(formData.workOrderId);
//           hh.value = false;
//         }
//       }
//     })
//     .catch(() => {
//       loading.value = false;
//     });
// };

// 选择加工单号
const oneTableList = ref([]);
// const changeWorkOrder = (e) => {
//   clearWorkOrder();
//   tableData.value.forEach((item) => {
//     if (item.id == e) {
//       console.log("加工单号", item);

//       formData.planBatch = item.planBatch;
//       formData.workOrderCode = item.workOrderCode;
//       formData.workOrderName = item.workOrderName;
//       oneTableList.value = [];
//       oneTableList.value.push(item);
//       getProcessList(item);
//       getDataDay();
//     }
//   });
// };
// // 改变年之后清空
// function clearWorkOrder() {
//   formData.workOrderName = "";
//   formData.workOrderCode = "";
//   formData.planBatch = "";
//   formData.planQuantity = 0;
//   formData.planAllQuantity = 0;
//   formData.processId = "";
//   formData.processCode = "";
//   formData.processName = "";
//   formData.equipmentId = "";
//   formData.workSequence = "";
//   formData.equipmentCode = "";
//   formData.equipmentName = "";
//   formData.planFinishQuantity = "";
//   formData.mesWorkOrderEquipId = "";
//   formData.mesWorkOrderPlanId = "";
//   formData.quantityUnit = "";
// }
// 根据加工单获取日期
// const dayList = ref([]);
// let kk = ref(true);
// const getDataDay = async () => {
//   const { data } = await request({
//     url: "/mes/messhiftschedule/getDateShiftCount",
//     method: "post",
//     data: {
//       planWeek: formData.planWeek,
//       planYear: formData.planYear,
//       workCenterId: formData.workCenterId,
//       plantId: formData.plantId,
//       planBatch: formData.planBatch,
//     },
//   });
//   dayList.value = data;
//   if (formData.id) {
//     if (kk.value) {
//       selectDay1(formData.planDate);
//       kk.value = false;
//     }
//   }
// };
// 选择日期
// 班次顺序
// const shiftCount = ref();
// const shiftList = ref([]);
// const selectDay = (e) => {
//   if (e) {
//     formData.workSequence = "";
//     shiftList.value = [];
//     dayList.value.forEach((item) => {
//       if (item.planDate == e) {
//         shiftCount.value = item.shiftCount;
//       }
//     });

//     for (let i = 0; i < shiftCount.value; i++) {
//       shiftList.value.push({ shiftName: i + 1 + "班", shiftCode: i + 1 });
//     }
//   } else {
//     formData.mesWorkOrderEquipId = "";
//     formData.mesWorkOrderPlanId = "";
//     formData.quantityUnit = "";
//   }
// };
// 班次选择
// const shiftAllList = ref([]);
// const selectShift = async (e) => {
//   if (e) {
//     // 获取班次列表
//     await request({
//       url: "/mes/mesworkorderplan/getList",
//       method: "post",
//       data: {
//         planWeek: formData.planWeek,
//         planYear: formData.planYear,
//         workOrderId: formData.workOrderId,
//         plantId: formData.plantId,
//         planBatch: formData.planBatch,
//         planDate: formData.planDate,
//         workSequence: e,
//       },
//     })
//       .then((res) => {
//         shiftAllList.value = res.data;
//         console.log("班次列表", shiftAllList.value);

//         if (shiftAllList.value.length > 0) {
//           formData.planQuantity = shiftAllList.value[0].planQuantity;

//           formData.planAllQuantity = shiftAllList.value[0].planFinishQuantity;

//           formData.quantityUnit = shiftAllList.value[0].quantityUnit;

//           formData.mesWorkOrderPlanId = shiftAllList.value[0].id;
//         } else {
//           formData.planQuantity = 0;
//           formData.planAllQuantity = 0;
//           formData.quantityUnit = "";
//           formData.mesWorkOrderPlanId = "";
//         }
//       })
//       .catch(() => {});
//   } else {
//     formData.planQuantity = 0;
//     formData.planAllQuantity = 0;
//     formData.quantityUnit = "";
//     formData.mesWorkOrderPlanId = "";
//   }
// };

// 获取工序列表
// 工序列表
const processList = ref([]);
const getProcessList = async (object: any) => {
  // 用这一行的数据作为参数 获取工序列表
  const { data } = await request({
    url: "/mes/mesworkorder/queryRoutingProcess",
    method: "post",
    data: object,
  });
  console.log("工序", data);

  processList.value = [];
  processList.value = data;
};

// 选择工作中心
// const changeWorkCenter = (e) => {
//   clearWorkCenter();
//   if (e) {
//     monthOptions.value = getMonthList(formData.planYear);
//     workShopList.value.forEach((item) => {
//       if (item.id == e) {
//         formData.workCenterCode = item.workshopCode;
//         formData.workCenterName = item.workshopName;
//         getTable();
//       }
//     });
//   } else {
//     tableData.value = [];
//     workOrderList.value = [];
//   }
// };
// function clearWorkCenter() {
//   formData.workCenterCode = "";
//   formData.workCenterName = "";
//   formData.planYear = currentYear;
//   formData.planWeek = currentWeek;
//   formData.workOrderId = "";
//   formData.workOrderName = "";
//   formData.workOrderCode = "";
//   formData.planBatch = "";
//   formData.planDate = "";
//   formData.workSequence = "";
//   formData.planQuantity = 0;
//   formData.planAllQuantity = 0;
//   formData.processId = "";
//   formData.processCode = "";
//   formData.processName = "";
//   formData.equipmentId = "";
//   formData.equipmentCode = "";
//   formData.equipmentName = "";
//   formData.planFinishQuantity = "";
//   formData.mesWorkOrderEquipId = "";
//   formData.mesWorkOrderPlanId = "";
//   formData.quantityUnit = "";
// }
// 选择工序
// 获取设备列表
const equipmentList = ref([]);
const changeProcess = (e) => {
  if (e) {
    processList.value.forEach((item) => {
      if (item.processId == e) {
        console.log("工序", item);
        formData.processCode = item.processCode;
        formData.processName = item.processName;
      }
    });
    equipmentList.value = [];
    oneTableList.value.forEach((item) => {
      item.workOrderEquipList.forEach((item1) => {
        if (item1.processId == e) {
          equipmentList.value.push(item1);
        }
      });
    });
  } else {
    equipmentList.value = [];
    formData.processCode = "";
    formData.processName = "";
    formData.equipmentName = "";
    formData.equipmentCode = "";
    formData.mesWorkOrderEquipId = "";
  }
};

// 选择设备
const changeEquipment = (e) => {
  if (e) {
    equipmentList.value.forEach((item) => {
      if (item.equipmentId == e) {
        console.log("设备", item);
        formData.mesWorkOrderEquipId = item.id;
        formData.equipmentName = item.equipmentName;
        formData.equipmentCode = item.equipmentCode;
      }
    });
  } else {
    formData.equipmentName = "";
    formData.equipmentCode = "";
    formData.mesWorkOrderEquipId = "";
  }
};

//工作任务选择弹窗显隐
const selectDialogSELECT = ref({
  visible: false,
});
const changeVisibleSELECT = (val: boolean, data: any) => {
  selectDialogSELECT.value.visible = val;
  if (data) {
    formData = Object.assign(formData, data);
    formData.mesWorkOrderPlanId = formData.id;
    formData.id = "";
    formData.planAllQuantity = formData.planFinishQuantity;
    formData.planFinishQuantity = "";
    formData.workCenters = `${formData.workCenterCode}-${formData.workCenterName}`;
    formData.workSequences = `${formData.workSequence}班`;
    formData.planDates = dayjs(formData.planDate).format("YYYY-MM-DD");
    formData.processId = "";
    formData.mesWorkOrderEquipId = "";
    formData.equipmentId = "";
    console.log("选择工作任务", formData);

    getPlanList();
  }
};

const brekList = ref([]);
const planList = ref([]);
const getPlanList = async () => {
  await request({
    url: "/mes/mesworkorder/queryByWeek",
    method: "post",
    data: {
      planWeek: formData.planWeek,
      planYear: formData.planYear,
      workCenterId: formData.workCenterId,
      plantId: formData.plantId,
      planBatch: formData.planBatch,
    },
  }).then((res) => {
    planList.value = [];
    planList.value = res.data;
    planList.value.forEach((item) => {
      if (formData.workOrderId == item.id) {
        getProcessList(item);
        oneTableList.value = [];
        oneTableList.value.push(item);
        console.log("oneTableList", oneTableList.value);
      }
    });
  });
};

const showWorkOrderSelect = () => {
  selectDialogSELECT.value.visible = true;
  if (formData.mesWorkOrderPlanId != "") {
    brekList.value.push(formData);
  }
};
onMounted(async () => {
  // 获取工厂列表
  plantList.value = await getPlantList();
  yearOptions.value = await getYearList();
});

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
